<?php
/**
 * To C参与活动用户控制器
 */
namespace Beerus\Modules\DesignerCompetition2021\Controllers;

use App\Http\Controllers\Controller;
use App\Pockets\GridDataModel as pGridDataModel;
use Doraemon\model\Article;
use Doraemon\model\DesignerCompetition2021\toc\ToCActionLog;
use Doraemon\model\DesignerCompetition2021\toc\ToCPrize;
use Doraemon\model\DesignerCompetition2021\toc\ToCUserInfo;
use Doraemon\service\DesignerCompetition2021\ToC;
use Doraemon\service\Member;
use Doraemon\tools\ImgUrl;
use Encore\Admin\Facades\Admin;
use Encore\Admin\Grid;
use Encore\Admin\Layout\Content;
use Illuminate\Http\Request;
use Encore\Admin\Widgets\Form;
use Encore\Admin\Widgets\Tab;
use Illuminate\Support\MessageBag;
use Doraemon\cache\DesignerCompetition2021\DcToC as cDcToC;

class ToCLogController extends Controller {
    const PAGE_SIZE = 20;

    public function index(Request $request, Content $content) {
        $page = $request->get('page', 1);
        $pageSize = $request->get('per_page', self::PAGE_SIZE);
        $request = $request->all();
        $condition = [];
        if ($request['uid']) {
            $condition['uid'] = $request['uid'];
        }
        if ($request['nick']) {
            $uid = \Doraemon\model\Member::getUidByNick($request['nick']);
            $condition['uid'] = $uid ?? '';
        }
        if ($request['action_type']) {
            $condition['action_type'] = $request['action_type'];
        }
        if ($request['add_time']['start']) {
            $condition['add_time[>=]'] = strtotime($request['add_time']['start']);
        }
        if ($request['add_time']['end']) {
            $condition['add_time[<=]'] = strtotime($request['add_time']['end']);
        }
        $total = ToCActionLog::count($condition);
        $condition['ORDER'] = ['id' => 'DESC'];
        $condition['LIMIT'] = [
            ($page - 1) * $pageSize,
            $pageSize
        ];
        $list = ToCActionLog::select('*', $condition);
        $this->formatList($list);
        $gridModel = new pGridDataModel($list, $total, [
            'perPage' => $pageSize
        ]);
        $gridList = Admin::grid(
            $gridModel,
            function (Grid $grid) use ($gridModel) {
                $grid->disableExport();
                $grid->disableRowSelector();
                $grid->filter(function (Grid\Filter $filter) {
                    $filter->disableIdFilter();
                    $filter->column(1 / 2, function ($filter) {
                        $filter->equal('uid', '用户UID')->placeholder('请输入uid');
                        $filter->equal('nick', '用户昵称')->placeholder('请输入昵称');
                    });
                    $filter->column(1 / 2, function ($filter) {
                        $filter->equal('action_type', '日志类型')->select(ToC::ACTION_TYPE_TXT);
                        $filter->between('add_time', '日志时间')->date();
                    });
                    $filter->expand();
                });
                $grid->column('id', 'ID');
                $grid->column('uid', 'UID')->width(150);
                $grid->column('nick', '昵称')->width(250);
                $grid->column('action_type', '操作类型')->display(function() {
                    if ($this->action_type == ToC::ACTION_TYPE_PLAY) {
                        return '<span style="color:deeppink">抽奖</span>';
                    }
                    if ($this->action_type == ToC::ACTION_TYPE_SUPPORT) {
                        return '<span style="color:green">助力</span>';
                    }
                    if ($this->action_type == ToC::ACTION_TYPE_SHARE) {
                        return '<span style="color:darkslategray">分享</span>';
                    }
                })->width(250);
                $grid->column('assoc_txt', '关联业务id')->width(250);
                $grid->column('action_data', '操作数据')->display(function() {
                    if ($this->action_type == ToC::ACTION_TYPE_SUPPORT) {
                        $actionData = json_decode($this->action_data, true);
                        return "助力来源：".ToC::CHANNEL_TXT[$actionData['channel']];
                    }
                })->width(250);
                $grid->column('add_time', '创建时间')->display(function() {
                    return date('Y-m-d H:i:s', $this->add_time);
                });
                $grid->disableActions();
            }
        );
        return $content
            ->header('TOC活动用户操作日志管理')
            ->row($gridList);
    }

    public function formatList(&$list) {
        $prizeList = ToCPrize::getAllPrizeList();
        $prizeList = array_combine(array_column($prizeList, 'id'), $prizeList);
        foreach ($list as &$item) {
            $item['nick'] = \Doraemon\model\Member::getNickByUid($item['uid']);
            if ($item['action_type'] == ToC::ACTION_TYPE_PLAY) {
                $item['assoc_txt'] = "获得奖品：".$prizeList[$item['assoc_id']]['name'];
            }
            if ($item['action_type'] == ToC::ACTION_TYPE_SUPPORT) {
                $item['assoc_txt'] = "被助力人：".\Doraemon\model\Member::getNickByUid($item['assoc_id']);;
            }
        }
        unset($item);
    }
}
